package com.store.app.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.store.app.R;
import com.store.app.bean.OrderBean;
import com.store.app.bean.OrderBuyBean;
import com.store.app.bean.WypDetailsListBean;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.List;

/**
 * Created by Administrator on 2016/12/6.
 */
public class OrderDetailsAdapter extends BaseAdapter {

    private Context mContext;
    private List<WypDetailsListBean> mDatas;

    public OrderDetailsAdapter(Context context, List<WypDetailsListBean> list) {
        this.mContext = context;
        this.mDatas = list;
    }

    @Override
    public int getCount() {
        if (mDatas != null) {
            return mDatas.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int i) {
        if (mDatas != null) {
            return mDatas.get(i);
        }
        return null;
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View convertView, ViewGroup viewGroup) {

        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = View.inflate(mContext, R.layout.item_order_details, null);
            holder.mIcon = (ImageView) convertView.findViewById(R.id.order_details_goods_icon);
            holder.mName = (TextView) convertView.findViewById(R.id.order_details_goods_name);
            holder.mNorms = (TextView) convertView.findViewById(R.id.order_details_goods_norms);
            holder.mPrice = (TextView) convertView.findViewById(R.id.order_details_goods_price);
            holder.mNum = (TextView) convertView.findViewById(R.id.order_details_goods_number);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        WypDetailsListBean orderBean = mDatas.get(i);
        String url;
        try {
            JSONArray jsonArray = new JSONArray(orderBean.getGoods_pic_info());
             url =  orderBean.getMap().get(jsonArray.getJSONObject(0).getString("path_id")).toString();
            ImageLoader.getInstance().displayImage(url, holder.mIcon, options);
            holder.mName.setText(orderBean.getGoods_title());
            holder.mNorms.setText(orderBean.getSpecification());
            holder.mPrice.setText(orderBean.getSale_price());
            holder.mNum.setText("x  " + orderBean.getQty());
        }catch (Exception e){e.printStackTrace();}


        return convertView;
    }

    class ViewHolder {
        ImageView mIcon;
        TextView mName;
        TextView mNorms;
        TextView mPrice;
        TextView mNum;
    }

    DisplayImageOptions options = new DisplayImageOptions.Builder()
            .cacheInMemory(true)
            .showImageOnLoading(R.drawable.default_picture_s)
            .showImageOnFail(R.drawable.default_picture_s)
            .showStubImage(R.drawable.default_picture_s)
            .imageScaleType(ImageScaleType.EXACTLY)
            .bitmapConfig(Bitmap.Config.RGB_565)
            .build();


}
